package com.aloda.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.aloda.annotation.OperateLogAnno;
import com.aloda.query.UserAnswerPageDTO;
import com.aloda.result.PageResult;
import com.aloda.result.Result;
import com.aloda.service.UserAnswerService;
import com.aloda.vo.userAnswer.UserAnswerInfoVO;
import com.aloda.vo.userAnswer.UserAnswerPageVO;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController("admin-answer")
@RequestMapping("/admin/answer")
public class UserAnswerController {

    @Resource
    private UserAnswerService userAnswerService;

    /**
     * 分页查询
     * @param userAnswerPageDTO dto
     * @return pageResult
     */
    @PostMapping("/page")
    public Result<PageResult<UserAnswerPageVO>> pageQuery(@RequestBody UserAnswerPageDTO userAnswerPageDTO) {
        PageResult<UserAnswerPageVO> pageResult = userAnswerService.pageQuery(userAnswerPageDTO);
        return Result.success(pageResult);
    }

    /**
     * 通过id获取
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<UserAnswerInfoVO> getById(@PathVariable Long id) {
        return Result.success(BeanUtil.copyProperties(userAnswerService.getById(id), UserAnswerInfoVO.class));
    }

    /**
     * 删除用户答题记录
     * @param ids
     * @return
     */
    @OperateLogAnno(category = "删除用户答题记录")
    @DeleteMapping
    public Result deleteUserAnswer(@RequestBody List<Long> ids) {
        return Result.success(userAnswerService.removeByIds(ids));
    }

    /**
     * 导出用户答题记录信息表格
     * @param response
     * @param ids
     */
    @OperateLogAnno(category = "导出用户答题记录信息表格")
    @PostMapping("/export")
    public void exportUserAnswerToExcel(HttpServletResponse response, @RequestBody List<Long> ids) {
        userAnswerService.exportToExcel(response, ids);
    }
}
